﻿<cfsilent>
	<cfsetting requesttimeout="90" />
	<cfscript>

		termId = event.getArg("TermID");
		courseId = event.getArg("CID");

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");

		/* 学籍相关信息 */
		studentId = sessionAdvice.getAutherUserID();
		//studentClassId = sessionAdvice.getUserProp("studentClass");
		//studentSubjectId = sessionAdvice.getUserProp("studentSubject");
		//studentDirId = sessionAdvice.getUserProp("studentSubDir");
		//studentGrade = sessionAdvice.getUserProp("studentGrade");
		//studentDepartment = sessionAdvice.getUserProp("studentDepartment");
		
		taskArray = [];
		chooserArray = [];

		if ( len(studentId) and len(termId) and len(courseId) ) {
				
			/* 根据学期和课程号读取计划课程可选教学任务列表 */
			
			/* TaskProps */
			/* 第3位 是否禁止学生选课 即老师已经开始成绩提交 或 考试安排已发布 */
			/* 第7位 是否撤班 */ 
			sql = "		SELECT 
							a.tsk_id, a.cidx, t_teacher.tch_name, t_campus.campus_name, a.task_week, 
							case when a.task_time is null then '-' else a.task_time end task_time, 
							case when a.task_place is null then '-' else a.task_place end task_place,
							a.task_limite, a.task_chooesd  
						FROM t_task a
							 INNER JOIN t_teacher ON t_teacher.tch_id = a.tch_id 
							 INNER JOIN t_campus ON t_campus.campus_id = a.campus_id 
						WHERE 
							a.term_id = :termId 
							AND 
							a.cid = :courseId 
							AND 
							SUBSTR(a.task_prop,3,1) = '0' 
							AND 
							SUBSTR(a.task_prop,7,1) = '0'  
						ORDER BY a.cidx ";
			queryObj = new Query( datasource=application.dnsSlave ); 
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			rs_task = queryObj.execute( sql=sql ).getResult();
			
			/* 根据学期和课程号读取单开班可选教学任务列表 */
			 
			sql = " SELECT 
							a.tsk_id, a.cidx, t_teacher.tch_name, t_campus.campus_name, a.task_week, 
							case when a.task_time is null then '-' else a.task_time end task_time, 
							case when a.task_place is null then '-' else a.task_place end task_place,
							a.task_limite, a.task_chooesd	  
					FROM t_reopen_task a 
						 INNER JOIN t_teacher ON a.tch_id = t_teacher.tch_id 
						 INNER JOIN t_campus ON a.campus_id = t_campus.campus_id  
					WHERE 
						a.term_id = :termId 
						AND 
						a.cid = :courseId 
						AND 
						SUBSTR(a.task_prop,3,1) = '0' 
						AND 
						SUBSTR(a.task_prop,7,1) = '0'  
					ORDER BY a.cidx";
			queryObj = new Query( datasource=application.dnsSlave ); 
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			rs_openTask = queryObj.execute( sql=sql ).getResult();					
			
			
			/* 检查学生是否已选某教学班 */
		 
			sql = " SELECT 
						   a.task_id, a.task_type  
					FROM t_student_course a 
					WHERE 
							a.term_id = :termId 
							and 
							a.cid = :courseId 
							and 
							a.stu_id = :studentId ";
			queryObj = new Query( datasource=application.dnsSlave ); 
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
			rs_chooser = queryObj.execute( sql=sql ).getResult();
								
			taskIdArray = [];
			taskTypeArray = [];
			
			
			for ( a=1; a LTE rs_chooser.recordCount; a++ ) {
				arrayAppend( taskIdArray,rs_chooser["task_id"][a]  );
				arrayAppend( taskTypeArray, rs_chooser["task_type"][a] );
			}
			
		}

		
	</cfscript>
</cfsilent>

<cfif rs_task.recordCount or rs_openTask.recordCount >
	<table class="UIEditable">
		<thead>
			<tr>
				<td width="25"></td>
				<td width="40">课序号</td>
				<td>教师</td>
				<td>开课方式</td>
				<td width="30">人数</td>
				<td width="30">席位</td>
				<td width="36">校区</td>
				<td width="36">周次</td>
				<td>上课时间</td>
				<td>上课地点</td>
			</tr>
		</thead>
		<tbody>
			<!--- 跟班 ---> 
				<cfloop query="rs_task">
				<cfset sitLeft = rs_task.task_limite-rs_task.task_chooesd />
				<tr class="editRows">
					<td>
						
						<cfif rs_chooser.recordCount GT 0 and arrayFind( taskIdArray, rs_task.tsk_id )>
								<span class="stat on"></span>
							<cfelseif sitLeft gt 0 and not arrayFind( taskTypeArray, "R" ) >
								<!--- 可选 且没有选单开班课程 --->
								<cfset string = courseId & rs_task.cidx & termId & "P" />
								<cfset key = rs_task.tsk_id />
								<cfset sign = encrypt(string, key, "PBEWithMD5AndDES", "Hex") />
								<a href="javascript:chooseCourse('<cfoutput>#courseId#</cfoutput>','<cfoutput>#rs_task.cidx#</cfoutput>','<cfoutput>#termId#</cfoutput>','P','<cfoutput>#rs_task.tsk_id#</cfoutput>', '<cfoutput>#sign#</cfoutput>');" class="stat off"></a>
							<cfelseif sitLeft gt 0>
								<span class="stat disable" title="已选不同开课类别的课程"></span>
							<cfelse>
								<span class="stat peoples" title="没有空余席位"></span>
						</cfif>
					</td>
					
					<td><span class="index"><cfoutput>#rs_task.cidx#</cfoutput></span></td>
					<td><cfoutput>#rs_task.tch_name#</cfoutput></td>
					<td>跟班</td>
					<td><span class="numeric"><cfoutput>#rs_task.task_limite#</cfoutput></span></td>
					<td><span class="numeric"><cfif sitLeft gt 0><cfoutput>#sitLeft#</cfoutput><cfelse>-/-</cfif></span></td>
					<td><cfoutput>#rs_task.campus_name#</cfoutput></td>
					<td><span class="numeric"><cfoutput>#rs_task.task_week#</cfoutput></span></td>
					<td><cfoutput>#replace(mathAdvice.convertTaskTime(rs_task.task_time), ",", "<br/>", "all")#</cfoutput></td>
					<td><cfoutput>#replace(rs_task.task_place, ",", "<br/>", "all")#</cfoutput></td>
				</tr>
			</cfloop>
			
			<!--- 单开班 ---> 
        		<cfloop query="rs_openTask" >
				<cfset sitLeft = rs_openTask.task_limite-rs_openTask.task_chooesd />
				<tr class="editRows">
					<td>
						<cfif rs_chooser.recordCount GT 0 and arrayFind( taskIdArray, rs_openTask.tsk_id )>
								<span class="stat on"></span>
							<cfelseif sitLeft gt 0 and not arrayFind( taskTypeArray, "P" ) >
								<!--- 可选 且没有选跟班课程 --->
								<cfset string = courseId & rs_openTask.cidx & termId & "R" />
								<cfset key = rs_openTask.tsk_id />
								<cfset sign = encrypt(string, key, "PBEWithMD5AndDES", "Hex") />
								<a href="javascript:chooseCourse('<cfoutput>#courseId#</cfoutput>','<cfoutput>#rs_openTask.cidx#</cfoutput>','<cfoutput>#termId#</cfoutput>','R','<cfoutput>#rs_openTask.tsk_id#</cfoutput>', '<cfoutput>#sign#</cfoutput>');" class="stat off"></a>
							<cfelseif sitLeft gt 0>
								<span class="stat disable" title="已选不同开课类别的课程"></span>
							<cfelse>
								<span class="stat peoples" title="没有空余席位"></span>
						</cfif>
					</td>
					
					<td><span class="index"><cfoutput>#rs_openTask.cidx#</cfoutput></span></td>
					<td><cfoutput>#rs_openTask.tch_name#</cfoutput></td>
					<td>单开班</td>
					<td><span class="numeric"><cfoutput>#rs_openTask.task_limite#</cfoutput></span></td>
					<td><span class="numeric"><cfif sitLeft gt 0><cfoutput>#sitLeft#</cfoutput><cfelse>-/-</cfif></span></td>
					<td><cfoutput>#rs_openTask.campus_name#</cfoutput></td>
					<td><span class="numeric"><cfoutput>#rs_openTask.task_week#</cfoutput></span></td>
					<td><cfoutput>#replace(mathAdvice.convertTaskTime(rs_openTask.task_time), ",", "<br/>", "all")#</cfoutput></td>
					<td><cfoutput>#replace(rs_openTask.task_place, ",", "<br/>", "all")#</cfoutput></td>
				</tr>
			</cfloop>
		</tbody>
	</table>
</cfif>
